package com.sanploy.manage.service.impl;


import com.sanploy.common.domain.Admin;
import com.sanploy.common.domain.dto.AdminDto;
import com.sanploy.common.domain.dto.Pages;
import com.sanploy.common.domain.vo.Page;
import com.sanploy.common.enums.AdminRole;
import com.sanploy.common.enums.Meta;
import com.sanploy.common.util.CommonUtil;
import com.sanploy.common.vo.BaseException;
import com.sanploy.manage.mapper.AdminMapper;
import com.sanploy.manage.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zhu yingzhi on 2017/10/16.
 * @author yingzhi zhu
 * 管理员 逻辑处理实现类
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public int addAdmin(AdminDto admin) throws BaseException {
        admin.setAdd_time(CommonUtil.getTimeNormal());
        return CommonUtil.judgeRUD(adminMapper.addAdmin(admin), Meta.InsertFail);
    }

    @Override
    public Admin findAdminById(Long id) throws BaseException {
        return adminMapper.findById(id);
    }

    @Override
    public Page<Admin> findAdmin(Pages pages) throws BaseException {
        Page<Admin> page = new Page<>();
        pages.setStart((pages.getCurrentPage()-1)*pages.getPageSize());
        //查找总数量
        int total = adminMapper.findAdminCount();
        page.setPageSize(pages.getPageSize());
        page.setCurrentPage(pages.getCurrentPage());
        page.setTotal(total);
        page.setTotalPage((int)Math.ceil((double)total/(double)pages.getPageSize()));

        page.setList(adminMapper.findAdmin(pages));
        page.getList().forEach(item->{
            //设置规则 枚举类型变为中文
            item.setRole(adminRoleToChinese(item));
        });
        return page;
    }

    @Override
    public int editAdmin(AdminDto adminDto) throws BaseException {
        return CommonUtil.judgeRUD(adminMapper.editAdmin(adminDto),Meta.UpdateFail);
    }

    @Override
    public int deleteAdmin(Long id) throws BaseException {
        return CommonUtil.judgeRUD(adminMapper.deleteAdmin(id),Meta.DeleteFail);
    }

    /**
     * 枚举类型常量变为中文 方法
     * @param admin 管理员信息
     * @return 变为中文的管理员权限
     */
    private String adminRoleToChinese(Admin admin){
        String[] arr = admin.getRole().split(",");
        StringBuilder roleString = new StringBuilder();
        for (String role :arr){

            switch (AdminRole.valueOf(role)){
                case SUPER:
                    roleString.append("超级管理员"+",");
                    break;
                case USER:
                    roleString.append("用户权限"+",");
                    break;
                case ORDER:
                    roleString.append("订单权限"+",");
                    break;
                case GOODS:
                    roleString.append("商品权限"+",");
                    break;
                case FREIGHT:
                    roleString.append("运费权限"+",");
                    break;
                case MESSAGE:
                    roleString.append("消息权限"+",");
                    break;
                case CATEGORY:
                    roleString.append("分类权限"+",");
                    break;
                case BANNER:
                    roleString.append("轮播图权限"+",");
                    break;
                default:break;
            }
        }
        return roleString.deleteCharAt(roleString.length()-1).toString();
    }
}
